﻿using System;
using System.Windows.Media.Imaging;
using SomethingSpacial.CustomControls;
using SomethingSpacial.Model;
using SomethingSpacial.Web;
using System.Collections.ObjectModel;
using System.Windows.Ria;

namespace SomethingSpacial.ViewModel
{
    public class UserGroupsViewModel : ViewModelBase
    {
        private UserGroupModel MyModel = new UserGroupModel();
        private SomethingSpacialContext domainContext = new SomethingSpacialContext();
        LoadOperation LoadUserGroups;

        public UserGroupsViewModel()
        {
            UserGroups = new ObservableCollection<UserGroupModel>();
        }


        public ObservableCollection<UserGroupModel> UserGroups
        {
            get;
            private set;
        }

        #region "ViewModel Methods"

        public void loadUserGroups()
        {
            LoadUserGroups = domainContext.Load(domainContext.GetUserGroupsFirstQuery());
            LoadUserGroups.Completed += new EventHandler(LoadUserGroups_Completed);
        }

        void LoadUserGroups_Completed(object sender, EventArgs e)
        {

            //Not sure why this is required.  Wouldn't it have to be "Complete" in order for this UserGroup to be triggered? (ds 09/26/09)
            if (LoadUserGroups.IsComplete == true)
            {
                foreach (Entity entity in LoadUserGroups.Entities)
                {
                    UserGroups UserGroup = entity as UserGroups;

                    var userGroup = new UserGroupModel();

                    userGroup.UserGroupID = UserGroup.UG_Id;
                    userGroup.ContactEmail = UserGroup.UG_ContactEmail;
                    userGroup.ContactName = UserGroup.UG_ContactName;
                    userGroup.EmailList = UserGroup.UG_EmailList;
                    userGroup.Facebook = UserGroup.UG_Facebook;
                    userGroup.NewsFeedSource = UserGroup.UG_NewsFeedSource;
                    userGroup.TweetSource = UserGroup.UG_TweetSource;
                    userGroup.UserGroupDescription = UserGroup.UG_Description;
                    userGroup.UserGroupName = UserGroup.UG_Name;
                    userGroup.UserGroupURL = UserGroup.UG_URL;
                    UserGroups.Add(userGroup);

                }
            }

        }

        public void addUserGroup(UserGroupsViewModel UserGroupNew)
        {
            try
            {
                UserGroups currUserGroup = new UserGroups();

                currUserGroup.UG_Id = 0;
                currUserGroup.UG_ContactEmail = UserGroupNew.ContactEmail;
                currUserGroup.UG_ContactName = UserGroupNew.ContactName;
                currUserGroup.UG_EmailList = UserGroupNew.EmailList;
                currUserGroup.UG_Facebook = UserGroupNew.Facebook;
                currUserGroup.UG_NewsFeedSource = UserGroupNew.NewsFeedSource;
                currUserGroup.UG_TweetSource = UserGroupNew.TweetSource;
                currUserGroup.UG_Description = UserGroupNew.UserGroupDescription;
                currUserGroup.UG_Name = UserGroupNew.UserGroupName;
                currUserGroup.UG_URL = UserGroupNew.UserGroupURL;

                domainContext.UserGroups.Add(currUserGroup);
                domainContext.SubmitChanges();
            }
            catch (Exception exc)
            {
                string message = exc.InnerException.ToString();
            }
        }

        #endregion

        #region "ViewModel Properties"

        public int UserGroupID
        {
            get { return MyModel.UserGroupID; }
            set
            {
                if (MyModel.UserGroupID != value)
                {
                    MyModel.UserGroupID = value;
                    InvokePropertyChanged("UserGroupID");
                }
            }
        }

        public string UserGroupDescription
        {
            get { return MyModel.UserGroupDescription; }
            set
            {
                if (MyModel.UserGroupDescription != value)
                {
                    MyModel.UserGroupDescription = value;
                    InvokePropertyChanged("UserGroupDescription");
                }
            }
        }

        public string UserGroupName
        {
            get { return MyModel.UserGroupName; }
            set
            {
                if (MyModel.UserGroupName != value)
                {
                    MyModel.UserGroupName = value;
                    InvokePropertyChanged("UserGroupName");
                }
            }
        }

        public string UserGroupURL
        {
            get { return MyModel.UserGroupURL; }
            set
            {
                if (MyModel.UserGroupURL != value)
                {
                    MyModel.UserGroupURL = value;
                    InvokePropertyChanged("UserGroupURL");
                }
            }
        }

        public string ContactName
        {
            get { return MyModel.ContactName; }
            set
            {
                if (MyModel.ContactName != value)
                {
                    MyModel.ContactName = value;
                    InvokePropertyChanged("ContactName");
                }
            }
        }

        public string ContactEmail
        {
            get { return MyModel.ContactEmail; }
            set
            {
                if (MyModel.ContactEmail != value)
                {
                    MyModel.ContactEmail = value;
                    InvokePropertyChanged("ContactEmail");
                }
            }
        }

        public string NewsFeedSource
        {
            get { return MyModel.NewsFeedSource; }
            set
            {
                if (MyModel.NewsFeedSource != value)
                {
                    MyModel.NewsFeedSource = value;
                    InvokePropertyChanged("NewsFeedSource");
                }
            }
        }

        public string TweetSource
        {
            get { return MyModel.TweetSource; }
            set
            {
                if (MyModel.TweetSource != value)
                {
                    MyModel.TweetSource = value;
                    InvokePropertyChanged("TweetSource");
                }
            }
        }

        public string Facebook
        {
            get { return MyModel.Facebook; }
            set
            {
                if (MyModel.Facebook != value)
                {
                    MyModel.Facebook = value;
                    InvokePropertyChanged("Facebook");
                }
            }
        }

        public string EmailList
        {
            get { return MyModel.EmailList; }
            set
            {
                if (MyModel.EmailList != value)
                {
                    MyModel.EmailList = value;
                    InvokePropertyChanged("EmailList");
                }
            }
        }

        #endregion
    }
}
